package cn.ly.core.aio;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * tcp协议发�?�数据：
 * a：创建发送端的socket对象
 * b：这�?步，就说明连接已经建立成功了
 * c：获取输出流，写数据
 * d：释放资�?
 * @author liuyu
 *
 * Tcp协议，必须先有服务端，否则有以下异常
 * java.net.ConnectException: Connection refused: connect
 *
 */


public class TcpClient {

	public static void main(String[] args) throws Exception {
		//创建发�?�端的socket对象
		//Socket(String host, int port) 
		Socket s = new Socket("localhost",8888);
		
		//获得输出�?
		OutputStream os = s.getOutputStream();
		os.write("hello,I am client".getBytes());
		
		os.flush();
		
		Thread.sleep(1000*5);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		
		String line = br.readLine();
		
		System.out.println("client:" + line);
		
		s.close();
	}
	
}
